package com.tdtxdcxm.mynotepad.slice;

import com.tdtxdcxm.mynotepad.ResourceTable;
import com.tdtxdcxm.mynotepad.listcontaineritem.NoteListItem;
import com.tdtxdcxm.mynotepad.notedata.NoteDataManager;
import com.tdtxdcxm.mynotepad.provider.NoteListItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;


public class MainAbilitySlice extends AbilitySlice {
    private ArrayList<NoteListItem> listcontainerlist = new ArrayList<>();
    private float x1,y1,x2,y2;

    private DirectionalLayout main_rootdl_ddl1,main_rootdl_ddl2,main_rootdl_ddl3;
    private DirectionalLayout main_rootdl_ddl1_1,main_rootdl_ddl1_2;
    private ListContainer main_rootdl_ddl2_listcontainer;

    private TextField main_rootdl_ddl1_1_tfd;
    private Button main_rootdl_ddl1_1_searchbut;

    private Text main_rootdl_ddl1_2_text;
    private Button main_rootdl_ddl1_2_selectbut,main_rootdl_ddl1_2_addbut;

    private Button main_rootdl_ddl3_nobut,main_rootdl_ddl3_delbut;

    public void toastShow(String s){
        ToastDialog toastDialog = new ToastDialog(this.getContext());
        toastDialog.setText(s);
        toastDialog.setTransparent(true);
        toastDialog.setDuration(100);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }

    public void initListContainer(){
        NoteDataManager.generateUserNoteDir();
        NoteDataManager.checkUserNoteDir();

        NoteDataManager.readAllNoteMataForList(listcontainerlist);

        NoteListItemProvider noteListItemProvider = new NoteListItemProvider(listcontainerlist,this);
        NoteListItemProvider.noteListItemProvider = noteListItemProvider;
        main_rootdl_ddl2_listcontainer.setItemProvider(noteListItemProvider);


        main_rootdl_ddl2_listcontainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent intent = new Intent();
                intent.setParam("type","detail");

                //将装有该笔记文本和图像的文件夹当作参数传入
                intent.setParam("thisnotefoldername",listcontainerlist.get(i).getFile().toString());
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.tdtxdcxm.mynotepad")
                        .withAbilityName("com.tdtxdcxm.mynotepad.EditAbility")
                        .build();
                intent.setOperation(operation);
                startAbilityForResult(intent,11);
            }
        });

    }

    public void initMASComponents(){
        main_rootdl_ddl1 = (DirectionalLayout) findComponentById(ResourceTable.Id_main_rootdl_ddl1);
        main_rootdl_ddl2 = (DirectionalLayout) findComponentById(ResourceTable.Id_main_rootdl_ddl2);
        main_rootdl_ddl3 = (DirectionalLayout) findComponentById(ResourceTable.Id_main_rootdl_ddl3);
        NoteListItemProvider.main_rootdl_ddl1 = main_rootdl_ddl1;

        main_rootdl_ddl1_1 = (DirectionalLayout) findComponentById(ResourceTable.Id_main_rootdl_ddl1_1);
        main_rootdl_ddl1_2 = (DirectionalLayout) findComponentById(ResourceTable.Id_main_rootdl_ddl1_2);

        main_rootdl_ddl2_listcontainer = (ListContainer) findComponentById(ResourceTable.Id_main_rootdl_ddl2_listcontainer);

        main_rootdl_ddl1_1_tfd = (TextField) findComponentById(ResourceTable.Id_main_rootdl_ddl1_1_tfd);
        main_rootdl_ddl1_1_searchbut = (Button) findComponentById(ResourceTable.Id_main_rootdl_ddl1_1_searchbut);

        main_rootdl_ddl1_2_text = (Text) findComponentById(ResourceTable.Id_main_rootdl_ddl1_2_text);
        main_rootdl_ddl1_2_selectbut = (Button) findComponentById(ResourceTable.Id_main_rootdl_ddl1_2_selectbut);
        main_rootdl_ddl1_2_addbut = (Button) findComponentById(ResourceTable.Id_main_rootdl_ddl1_2_addbut);


        main_rootdl_ddl3_nobut = (Button) findComponentById(ResourceTable.Id_main_rootdl_ddl3_nobut);
        main_rootdl_ddl3_delbut = (Button) findComponentById(ResourceTable.Id_main_rootdl_ddl3_delbut);

        /******************************************搜索按钮监听器*******************************************/
        main_rootdl_ddl1_1_searchbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                main_rootdl_ddl1_1_tfd.clearFocus();
                String text = main_rootdl_ddl1_1_tfd.getText();
                if(listcontainerlist.size() != 0){
                    for(int i = 0;i < listcontainerlist.size();i++){
                        String s = listcontainerlist.get(i).getText();
                        if(s.indexOf(text) != -1){
                            Intent intent = new Intent();
                            intent.setParam("type","detail");

                            //将装有该笔记文本和图像的文件夹当作参数传入
                            intent.setParam("thisnotefoldername",listcontainerlist.get(i).getFile().toString());
                            Operation operation = new Intent.OperationBuilder()
                                    .withDeviceId("")
                                    .withBundleName("com.tdtxdcxm.mynotepad")
                                    .withAbilityName("com.tdtxdcxm.mynotepad.EditAbility")
                                    .build();
                            intent.setOperation(operation);
                            startAbilityForResult(intent,11);
                            return;
                        }
                    }
                    main_rootdl_ddl1_1_tfd.setText("");
                    toastShow("未找到该关键字匹配的笔记！");
                }
                else{
                    main_rootdl_ddl1_1_tfd.setText("");
                    toastShow("当前无任何笔记！请新建笔记！");
                }
            }
        });
        /******************************************搜索按钮监听器*******************************************/

        /****************************************标题文本按钮监听器*****************************************/
        main_rootdl_ddl1_2_text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                main_rootdl_ddl1_1_tfd.clearFocus();
            }
        });
        /****************************************标题文本按钮监听器*****************************************/

        /*<********************************给2个功能按键设置监听器**********************************************/
        main_rootdl_ddl1_2_selectbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                main_rootdl_ddl1_1_tfd.clearFocus();
                main_rootdl_ddl3.setVisibility(Component.VISIBLE);

                int count = NoteListItemProvider.noteListItemProvider.getCount();
                for(int i = 0;i < count;i++){
                    DirectionalLayout directionalLayout = (DirectionalLayout) main_rootdl_ddl2_listcontainer.getComponentAt(i);
                    directionalLayout.getComponentAt(0).setVisibility(Component.VISIBLE);
                }

            }
        });
        main_rootdl_ddl1_2_addbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                main_rootdl_ddl1_1_tfd.clearFocus();

                Intent intent = new Intent();
                intent.setParam("type","add");
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.tdtxdcxm.mynotepad")
                        .withAbilityName("com.tdtxdcxm.mynotepad.EditAbility")
                        .build();
                intent.setOperation(operation);
                startAbilityForResult(intent,10);

            }
        });
        /*********************************给2个功能按键设置监听器********************************************>*/

        /*<********************************给listContainer设置触摸滑动监听器******************************************/

        main_rootdl_ddl2_listcontainer.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                main_rootdl_ddl1_1_tfd.clearFocus();

                int action = touchEvent.getAction();
                if(action == TouchEvent.PRIMARY_POINT_DOWN){
                    MmiPoint mmiPoint = touchEvent.getPointerPosition(0);
                    x1 = mmiPoint.getX();
                    y1 = mmiPoint.getY();
                }
                if(action == TouchEvent.PRIMARY_POINT_UP){
                    MmiPoint mmiPoint = touchEvent.getPointerPosition(0);
                    x2 = mmiPoint.getX();
                    y2 = mmiPoint.getY();

                    if(y1 > y2 && Math.abs(x2 - x1) <= 90.0){
                        main_rootdl_ddl1.setVisibility(Component.HIDE);
                        //main_rootdl_ddl3.setVisibility(Component.HIDE);
                    }
                    if(y1 < y2 && Math.abs(x2 - x1) <= 90.0){
                        main_rootdl_ddl1.setVisibility(Component.VISIBLE);
                        //main_rootdl_ddl3.setVisibility(Component.VISIBLE);
                    }
                }
                return true;
            }
        });
        /**********************************给listContainer设置触摸滑动监听器****************************************>*/

        /*<********************************给底部两按钮设置监听器******************************************/
        main_rootdl_ddl3_nobut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                main_rootdl_ddl3.setVisibility(Component.HIDE);
                int count = NoteListItemProvider.noteListItemProvider.getCount();
                for(int i = 0;i < count;i++){
                    DirectionalLayout directionalLayout = (DirectionalLayout) main_rootdl_ddl2_listcontainer.getComponentAt(i);
                    Text text = (Text)  directionalLayout.getComponentAt(0);
                    text.setVisibility(Component.HIDE);
                    text.setText("N");
                    text.setTextColor(Color.WHITE);
                }
            }
        });
        main_rootdl_ddl3_delbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(main_rootdl_ddl2_listcontainer.getChildCount() != 0){

                    int n = main_rootdl_ddl2_listcontainer.getChildCount();
                    System.out.println("n----->>>>"+n);
                    ArrayList<NoteListItem> t = new ArrayList<>();

                    for(int i = 0; i< n;i++){
                        DirectionalLayout directionalLayout = (DirectionalLayout) main_rootdl_ddl2_listcontainer.getComponentAt(i);
                        Text text = (Text)  directionalLayout.getComponentAt(0);
                        if(text.getText().equals("✔")){
                            NoteDataManager.deleteFolderFiles(listcontainerlist.get(i).getFile());
                        }
                        else{
                            t.add(listcontainerlist.get(i));
                        }
                    }
                    listcontainerlist.clear();
                    for(int i = 0;i < t.size();i++){
                        listcontainerlist.add(t.get(i));
                    }
                    NoteListItemProvider.noteListItemProvider.notifyDataChanged();
                    System.out.println("已经刷新数据了！！！！！！！！！");
                    t.clear();
                }
            }
        });
        /*<********************************给底部两按钮设置监听器******************************************/

    }

    @Override
    public void onAbilityResult(int requestCode, int resultCode, Intent resultData){
        System.out.println("回到了MainAbility中！");
        NoteDataManager.readAllNoteMataForList(listcontainerlist);
        NoteListItemProvider.noteListItemProvider.notifyDataChanged();
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initMASComponents();
        initListContainer();

    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
